package cn.aib.eduservice.service.impl;

import cn.aib.commonutils.R;
import cn.aib.eduservice.client.VodClient;
import cn.aib.eduservice.entity.EduVideo;
import cn.aib.eduservice.mapper.EduVideoMapper;
import cn.aib.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-21
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;

    @Override
    public R removeVideoList(String courseId) {
        //删除课程的所有视频
            //获取所有视频ID
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();

        videoWrapper.eq("course_id", courseId);

        videoWrapper.select("video_source_id");

        //删除视频
        List<EduVideo> videos = baseMapper.selectList(videoWrapper);

        List<String> ids = new ArrayList<>();

        if(videos.size() > 0){
            for (int i = 0; i < videos.size(); i++) {
                String videoSourceId = videos.get(i).getVideoSourceId();

                if(!StringUtils.isEmpty(videoSourceId)){
                    ids.add(videoSourceId);
                }
            }
        }

        if(ids.size()>0){
            R result = vodClient.deleteBatchVideo(ids);
            return result;
        }

        //删除小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();

        videoQueryWrapper.eq("course_id", courseId);

        baseMapper.delete(videoQueryWrapper);

        return R.ok();
    }
}
